package com.kaili.finance.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.finance.pojo.entity.*;
import com.kaili.finance.pojo.dto.customerBill.FinCheckBillQueryDTO;
import com.kaili.finance.pojo.dto.driver.FinDriverBatchCalcDTO;
import com.kaili.finance.pojo.dto.driver.FinDriverCalcQueryDTO;
import com.kaili.finance.pojo.dto.siteCalc.FinSiteCalcQueryDTO;
import com.kaili.finance.pojo.dto.transfer.FinTransferCalcQueryDTO;
import com.kaili.finance.pojo.vo.*;
import com.kaili.finance.service.FinCheckBillService;
import com.kaili.finance.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by :zengshuai
 * Created time :18-5-22.
 * Company :杭州凯立通信有限公司
 */

@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "财务结算", description = "财务结算相关API")
public class FinCalcController {

    @Autowired
    FinCheckBillService CheckBillService;

    @GetMapping("/customerBills")
    @ApiOperation(value = "客户对账单信息条件分页查询",notes = "条件分页查询客户对账单信息API")
    public Page<FinCheckBillVO> selectCheckBill(@ModelAttribute FinCheckBillQueryDTO dto) {
        return CheckBillService.selectCheckBill(new Page<FinCheckBillVO>(dto.getPage(),dto.getPageSize()),dto);
    }

    @PostMapping("/customerBills/bat")
    @ApiOperation(value = "客户对账单批量结算",notes = "客户对账单批量结算API")
    @Transactional
    public RestStatus batchCheckBillCalc(@Validated @RequestBody FinDriverBatchCalcDTO dto) {
        Date calcTime = new Date();
        Map<String,Object> map = new HashMap<String, Object>();
        for (int i = 0; i < dto.getOperationIds().length; i++) {
            map.put("operationId",dto.getOperationIds()[i]);
            map.put("payModeId",dto.getPayModeId());
            map.put("calcBy", LoginDetailUtils.getCurrentEmployeeId());
            map.put("calcTime",calcTime);
            map.put("sourceType",'3');
            if(CheckBillService.checkBillCalc(map)==0){
                return CommonStatus.Calc_FAIL;
            }
        }
        return CommonStatus.Calc_OK;
    }

    @GetMapping("/drivers")
    @ApiOperation(value = "驾驶员结算条件分页查询",notes = "驾驶员结算条件分页查询API")
    public Page<FinDriverCalcVO> queryDriverByKeywords(@ModelAttribute FinDriverCalcQueryDTO dto) {
        return CheckBillService.queryDriverByKeywords(new Page<FinDriverCalcVO>(dto.getPage(),dto.getPageSize()),dto);
    }

    @PostMapping("/drivers/bat")
    @ApiOperation(value = "驾驶员批量结算",notes = "驾驶员结算API")
    @Transactional
    public RestStatus batchDriverCalc(@Validated @RequestBody FinDriverBatchCalcDTO dto) {
        Date calcTime = new Date();
        Map<String,Object> map = new HashMap<String, Object>();
        for (int i = 0; i <dto.getOperationIds().length; i++) {
            map.put("operationId",dto.getOperationIds()[i]);
            map.put("payModeId",dto.getPayModeId());
            map.put("calcBy", LoginDetailUtils.getCurrentEmployeeId());
            map.put("calcTime",calcTime);
            map.put("sourceType",'4');
            if(CheckBillService.driverCalc(map) == 0){
                return CommonStatus.Calc_FAIL;
            }
        }
        return CommonStatus.Calc_OK;
    }

    @GetMapping("/transfers")
    @ApiOperation(value = "中转外包结算条件分页查询",notes = "中转外包结算条件分页查询API")
    public Page<FinTransferCalcVO> queryTransferByKeywords(@ModelAttribute FinTransferCalcQueryDTO dto) {
          return CheckBillService.queryTransferByKeywords(new Page<FinTransferCalcVO>(dto.getPage(),dto.getPageSize()),dto);
    }

    @PostMapping("/transfers/bat")
    @ApiOperation(value = "中转外包批量结算",notes = "中转外包批量结算API")
    @Transactional
    public RestStatus batchTransferCalc(@Validated @RequestBody FinDriverBatchCalcDTO dto){
        Date calcTime = new Date();
        Map<String,Object> map = new HashMap<String, Object>();
        for (int i = 0; i < dto.getOperationIds().length; i++) {
            map.put("operationId",dto.getOperationIds()[i]);
            map.put("payModeId",dto.getPayModeId());
            map.put("calcBy", LoginDetailUtils.getCurrentEmployeeId());
            map.put("calcTime",calcTime);
            map.put("sourceType",'5');
            if(CheckBillService.transferCalc(map)==0){
                return CommonStatus.Calc_FAIL;
            }
        }
        return CommonStatus.Calc_OK;
    }

    @GetMapping("/siteCalcs")
    @ApiOperation(value = "站点间结算条件分页查询",notes = "站点间结算条件分页查询API")
    public Page<FinSiteCalcVO> querySiteCalc(@ModelAttribute FinSiteCalcQueryDTO dto) {
        return CheckBillService.querySiteCalc(new Page<FinSiteCalcVO>(dto.getPage(),dto.getPageSize()),dto);
    }

    @PostMapping("/siteCalcs/bat")
    @ApiOperation(value = "站点间批量结算",notes = "站点间批量结算API")
    @Transactional
    public RestStatus batchSiteCalc(@Validated @RequestBody FinDriverBatchCalcDTO dto){
        Date calcTime = new Date();
        Map<String,Object> map = new HashMap<String, Object>();
        for (int i = 0; i < dto.getOperationIds().length; i++) {
            map.put("operationId",dto.getOperationIds()[i]);
            map.put("payModeId",dto.getPayModeId());
            map.put("calcBy", LoginDetailUtils.getCurrentEmployeeId());
            map.put("calcTime",calcTime);
            map.put("sourceType",'6');
            if(CheckBillService.siteCalc(map)==0){
                return CommonStatus.Calc_FAIL;
            }
        }
        return CommonStatus.Calc_OK;
    }


    //获取支付方式
    @GetMapping("/payMode")
    @ApiOperation(value = "获取支付方式",notes = "获取支付方式API")
    public List<FinDictVO> queryAllPayMent(){
        return CheckBillService.queryAllPayMent();
    }
}
